package com.hl.controller;

import com.hl.domain.Article;
import com.hl.service.ArticleService;
import com.hl.util.R;
import org.apache.ibatis.io.Resources;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @RequestMapping("/findAll")
    public Map<String, Object> findAll(int pageNum1,int pageSize1){
         return articleService.findAll(pageNum1,pageSize1);
    }

    @GetMapping("/findAllByTitle")
    public List<Article> findAllByTitle(String title){
        return articleService.findAllByTitle(title);
    }

    @RequestMapping("/add")
    public int add(Article article){
        return articleService.add(article);
    }

     @RequestMapping("/delById")
     public int delById(int id){
        return articleService.delById(id);
    }

//    图片上传
    @RequestMapping("/upload")
    public R upload(MultipartFile file) throws IOException {
        String path = "D:\\upload\\";
        String originalFilename = file.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
        String filename = UUID.randomUUID().toString() + ext;
        path += filename;
        file.transferTo(new File(path));
        System.out.println("path==========-------");
        System.out.println(path);
        return R.success(filename);
    }

    //    修改
//    根据id内容显示到前端
    @RequestMapping("/findById")
   public Article findById(int id){
        return articleService.findById(id);
    }
    //    根据id修改
    @RequestMapping("/updateById")
    public int updateById(Article article) {

        return articleService.updateById(article);
    }

}
